package com.rzjm.exercise.sys.system.login.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.constant.Constant;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.common.utils.ShiroUtils;
import com.rzjm.exercise.common.utils.StringUtils;
import com.rzjm.exercise.common.utils.encryp.impl.MD5Utils;
import com.rzjm.exercise.module.domain.*;
import com.rzjm.exercise.module.service.*;
import com.rzjm.exercise.sys.core.file.service.FileService;
import com.rzjm.exercise.sys.system.dept.service.DeptService;
import com.rzjm.exercise.sys.system.menu.domain.MenuDO;
import com.rzjm.exercise.sys.system.menu.service.MenuService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import com.rzjm.exercise.sys.system.user.service.UserService;
import com.rzjm.exercise.sys.vo.Tree;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

@Controller
public class LoginController extends BaseController {
	@Resource
	private UserService userService;
	@Resource
	private DeptService sysDeptService;
	@Resource
	private CitizenService citizenService;
	@Resource
	private TownsService townsService;
	@Resource
	private SurveyUserRecordService surveyUserRecordService;
	@Resource
	private MenuService menuService;
	@Resource
	private FileService fileService;
	@Resource
	private ParameterService parameterService;
	@Resource
	private ShopService shopService;
	@Resource
	UserShopRelationService userShopRelationService;
	@Resource
	private TerminalCheckRecordService terminalCheckRecordService;
	@Resource
	private CkTerminalService ckTerminalService;
	@Resource
	private DeviceWeldingService deviceWeldingService;
	@Resource
	private PianqusService pianqusService;
	@Resource
	private GroupsService groupsService;

	@Value("${projecturl.imgurl}")
	private String imgUrl;

	@Value("${spring.profiles.active}")
	private String active;

	/**
	 *  登录请求
	 * @param model
	 * @param code
	 * @return
	 */
	@GetMapping(value = {"/login/{code}","/login"})
	String login(Model model, @PathVariable(value = "code", required = false) String code) {
		Parameter parameter = parameterService.selectByUniqueColumn("is_default","1");
		model.addAttribute("parameter", parameter);
		if(code == null){
			code = "admin";
		}
		if(code.equals("favicon.ico")){
			code = "admin";
		}

		Groups groups = groupsService.selectByUniqueColumn("group_code", code);
		if (groups != null) {
			model.addAttribute("bg_url", imgUrl + groups.getBgUrl());
			if (StringUtils.isNotBlank(groups.getLogoUrl())) {
				model.addAttribute("logo_url", imgUrl + groups.getLogoUrl());
			}
			model.addAttribute("groupName", groups.getGroupName());
			model.addAttribute("group_id", groups.getId());
			model.addAttribute("group_code",groups.getGroupCode());
			model.addAttribute("logo_width",groups.getLogoWidth());
			model.addAttribute("logo_height",groups.getLogoHeight());
		}
		if(active.equals("yinhuan")){
			return "login_yinhuan";
		}else{
			return "login";
		}

	}


	@Log("登录")
	@PostMapping("/login")
	@ResponseBody
	R ajaxLogin(String username, String password) {

		password = MD5Utils.encrypt(username, password);
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		Subject subject = SecurityUtils.getSubject();
		try {
			Session session = subject.getSession();
			subject.login(token);
			UserDO user = userService.getPwd(username,password);
			if(user != null){
				if(user.getStatus() != 1){
					return R.error("该账号已禁用");
				}
			}
			session.setAttribute(Constant.USER,user);
			Map<String, Object> map = new HashMap<>();
			map.put("user",user);
			return R.ok(map);
		} catch (AuthenticationException e) {
			logger.error(e.toString());
			return R.error(e.getMessage());
		}
	}

	@GetMapping({"/", "" })
	String welcome(Model model) {
		return "redirect:login";
	}

	@Log("请求访问主页")
	@GetMapping({ "/index" })
	String index(Model model,String group_code, Integer goType) {
		UserDO user = getUser();
		List<Tree<MenuDO>> menus = menuService.listMenuTree(getUserId());
		model.addAttribute("menus", menus);
		model.addAttribute("name", user.getName());
		model.addAttribute("phone", user.getMobile());
		model.addAttribute("picUrl","img/logo.png");
		model.addAttribute("username", user.getUsername());
		model.addAttribute("userType", user.getUserType());
		model.addAttribute("roleId", user.getRoleId());
		model.addAttribute("shopName", "");
		model.addAttribute("controlText", "");
		if(user.getShopId()!=null && user.getShopId() > 0){
			Shop shop = shopService.findById(user.getShopId());
			if(shop!=null){
				model.addAttribute("shopName", shop.getShopName());
				model.addAttribute("controlText", shop.getControlText());
			}
		}

		if(user.getUserType() == 7 || user.getUserType() == 9){
			model.addAttribute("shopName", "沂南县应急管理局");
		}else if(user.getUserType() == 2){
			model.addAttribute("shopName", "制粒机测温管理系统");
		} else if( user.getUserType() == 13 || user.getUserType() == 14){
			model.addAttribute("shopName", "制粒机测温管理系统");
		}

		Groups groups = null;
		if(user.getUserType() == 2){
			groups = groupsService.findById(user.getGroupId());
		}else{
			if(user.getUserId() == 1){
				group_code = "admin";
			}
			groups = groupsService.selectByUniqueColumn("group_code",group_code);
		}
		if(groups != null){
			if(user.getUserType() == 2) {
				model.addAttribute("shopName", groups.getGroupName());
			}
			model.addAttribute("groupName", groups.getGroupName());
			model.addAttribute("bg_url",imgUrl + groups.getBgUrl());
			model.addAttribute("logo_url",imgUrl + groups.getLogoUrl());
			model.addAttribute("group_id",groups.getId());
			model.addAttribute("group_code",groups.getGroupCode());
			model.addAttribute("logo_width",groups.getLogoWidth());
			model.addAttribute("logo_height",groups.getLogoHeight());
		}

		Parameter parameter = parameterService.selectByUniqueColumn("is_default","1");
		model.addAttribute("parameter", parameter);
		if(goType == null && (user.getUserType() == 2 || user.getRoleId().intValue() == 5)){
			if(user.getGroupId() != null && user.getGroupId().intValue() == 10){
				return "haiding";
			}else {
				return "guide";
			}
		}
		String type = "";
		if (goType == null) {
			goType = 1;
		}
		switch (goType) {
			case 1:
				type = "main";
				break;
			case 2:
				type = "module/fireWork/0";
				break;
			case 3:
				type = "module/device/welding/record";
				break;
			case 4:
				type = "module/fireWork/0";
				break;
			case 5:
				type = "module/device/welding/shop/0";
				break;
			case 6:
				type = "module/device/4";
				break;
			case 7:
				type = "module/device/range/shop/list/0";
				break;
			case 8:
				type = "module/device/range/shop/list/1";
				break;
			default:
				type = "main";
		}
		model.addAttribute("goType", type);
		if(user.getUserType() == 13 || user.getUserType() == 14){
			return "index_zhili";
		} else{
			if(active.equals("yinhuan")){
				return "index_yinhuan";
			}else{
				return "index";
			}
		}
	}

	@GetMapping("/logout/{code}")
	String logout(@PathVariable("code") String code) {
		ShiroUtils.logout();
		if(StrUtil.isNotEmpty(code) && !code.equals("null")){
			return "redirect:/login/" + code;
		}else{
			return "redirect:/login";
		}
//		return "redirect:/login?code=" + code;
	}

	@GetMapping("/main")
	String main(Model model) {
		UserDO user = getUser();
		model.addAttribute("userType",user.getUserType());
		if(user.getUserType() == 0 || user.getUserType() == 1){//平台管理员
			QueryWrapper<UserDO> qw = new QueryWrapper<>();
			qw.select("user_id");
			qw.eq("user_type",2);
			long jituanCount = userService.selectCountByQueryWrapper(qw);
			model.addAttribute("jituan_user_count",jituanCount);

			QueryWrapper<TerminalCheckRecord> qwRecord = new QueryWrapper<>();
			qwRecord.select("id");
			qwRecord.eq("is_del",0);
			long checkRecordCount = terminalCheckRecordService.count(qwRecord);
			model.addAttribute("check_record_count",checkRecordCount);

			QueryWrapper<Shop> qwShop = new QueryWrapper<>();
			qwShop.select("id");
			long shopCount = shopService.count(qwShop);
			model.addAttribute("shop_count",shopCount);

			QueryWrapper<CkTerminal> qwTerminal = new QueryWrapper<>();
			qwTerminal.select("id");
			long terminalCount = ckTerminalService.count(qwTerminal);
			model.addAttribute("terminal_count",terminalCount);

		} else if(user.getUserType() == 2 ){//集团管理员

			Map<String,Object> map = new HashMap<>();
			map.put("group_id", user.getGroupId());
			map.put("device_type",0);
			Integer device0Count = deviceWeldingService.getDeviceCountByUserId(map);
			model.addAttribute("device0Count",device0Count);


			Map<String,Object> mapUse0 = new HashMap<>();
			mapUse0.put("group_id", user.getGroupId());
			mapUse0.put("device_type",0);
			mapUse0.put("use_state", 2);
			Integer deviceUse0Count = deviceWeldingService.getDeviceCountByUserId(mapUse0);
			model.addAttribute("deviceUse0Count",deviceUse0Count);


			Map<String,Object> map1 = new HashMap<>();
			map1.put("group_id", user.getGroupId());
			map1.put("device_type",1);
			Integer device1Count = deviceWeldingService.getDeviceCountByUserId(map1);
			model.addAttribute("device1Count",device1Count);


			Map<String,Object> mapUse1 = new HashMap<>();
			mapUse1.put("group_id", user.getGroupId());
			mapUse1.put("device_type", 1);
			mapUse1.put("use_state", 2);
			Integer deviceUse1Count = deviceWeldingService.getDeviceCountByUserId(mapUse1);
			model.addAttribute("deviceUse1Count",deviceUse1Count);


			QueryWrapper<Pianqus> qwPian = new QueryWrapper<>();
			qwPian.eq("jituan_id", user.getGroupId());
			long pianquCount = pianqusService.count(qwPian);
			model.addAttribute("pianquCount",pianquCount);


		} else if(user.getUserType() == 3){//片区管理员

			Map<String,Object> map = new HashMap<>();
			map.put("group_id", user.getGroupId());
			map.put("pianqu_id", user.getPianquId());
			map.put("device_type",0);
			Integer device0Count = deviceWeldingService.getDeviceCountByUserId(map);
			model.addAttribute("device0Count",device0Count);


			Map<String,Object> mapUse0 = new HashMap<>();
			mapUse0.put("group_id", user.getGroupId());
			mapUse0.put("pianqu_id", user.getPianquId());
			mapUse0.put("device_type",0);
			mapUse0.put("use_state", 2);
			Integer deviceUse0Count = deviceWeldingService.getDeviceCountByUserId(mapUse0);
			model.addAttribute("deviceUse0Count",deviceUse0Count);


			Map<String,Object> map1 = new HashMap<>();
			map1.put("group_id", user.getGroupId());
			map1.put("pianqu_id", user.getPianquId());
			map1.put("device_type",1);
			Integer device1Count = deviceWeldingService.getDeviceCountByUserId(map1);
			model.addAttribute("device1Count",device1Count);


			Map<String,Object> mapUse1 = new HashMap<>();
			mapUse1.put("group_id", user.getGroupId());
			mapUse1.put("pianqu_id", user.getPianquId());
			mapUse1.put("device_type",1);
			mapUse1.put("use_state", 2);
			Integer deviceUse1Count = deviceWeldingService.getDeviceCountByUserId(mapUse1);
			model.addAttribute("deviceUse1Count",deviceUse1Count);


			QueryWrapper<Pianqus> qwPian = new QueryWrapper<>();
			qwPian.eq("jituan_id", user.getGroupId());
			long pianquCount = pianqusService.count(qwPian);
			model.addAttribute("pianquCount",pianquCount);

		} else if(user.getUserType() == 4 || user.getUserType() == 5 || user.getUserType() == 6){//分厂  巡检人员

			Map<String,Object> map = new HashMap<>();
			map.put("shop_id", user.getShopId());
			map.put("device_type",0);
			Integer device0Count = deviceWeldingService.getDeviceCountByUserId(map);
			model.addAttribute("device0Count",device0Count);


			Map<String,Object> mapUse0 = new HashMap<>();
			mapUse0.put("shop_id", user.getShopId());
			mapUse0.put("device_type",0);
			mapUse0.put("use_state", 2);
			Integer deviceUse0Count = deviceWeldingService.getDeviceCountByUserId(mapUse0);
			model.addAttribute("deviceUse0Count",deviceUse0Count);


			Map<String,Object> map1 = new HashMap<>();
			map1.put("shop_id", user.getShopId());
			map1.put("device_type",1);
			Integer device1Count = deviceWeldingService.getDeviceCountByUserId(map1);
			model.addAttribute("device1Count",device1Count);


			Map<String,Object> mapUse1 = new HashMap<>();
			mapUse1.put("shop_id", user.getShopId());
			mapUse1.put("device_type",1);
			mapUse1.put("use_state", 2);
			Integer deviceUse1Count = deviceWeldingService.getDeviceCountByUserId(mapUse1);
			model.addAttribute("deviceUse1Count",deviceUse1Count);


			QueryWrapper<Pianqus> qwPian = new QueryWrapper<>();
			qwPian.eq("jituan_id", user.getGroupId());
			long pianquCount = pianqusService.count(qwPian);
			model.addAttribute("pianquCount",pianquCount);

//			QueryWrapper<UserDO> qw = new QueryWrapper<>();
//			qw.select("user_id");
//			qw.eq("shop_id",user.getShopId());
//			qw.eq("user_type",5);
//			long checkUserCount = userService.selectCountByQueryWrapper(qw);
//			model.addAttribute("check_user_count",checkUserCount);
//
//			QueryWrapper<TerminalCheckRecord> qw1 = new QueryWrapper<>();
//			qw1.select("id");
//			qw1.eq("shop_id",user.getShopId());
//			qw1.eq("is_clean",0);
//			qw1.eq("is_del",0);
//			long checkRecordCount = terminalCheckRecordService.selectCountByQueryWrapper(qw1);
//			model.addAttribute("check_record_count",checkRecordCount);
//
//			QueryWrapper<TerminalCheckRecord> qw2 = new QueryWrapper<>();
//			qw2.select("id");
//			qw2.eq("shop_id",user.getShopId());
//			qw2.eq("is_clean",1);
//			qw2.eq("is_del",0);
//			long cleanRecordCount = terminalCheckRecordService.selectCountByQueryWrapper(qw2);
//			model.addAttribute("clean_record_count",cleanRecordCount);
//
//			QueryWrapper<CkTerminal> qw3 = new QueryWrapper<>();
//			qw3.select("id");
//			qw3.eq("shop_id",user.getShopId());
//			long terminalCount = ckTerminalService.selectCountByQueryWrapper(qw3);
//			model.addAttribute("terminal_count",terminalCount);
		} else if(user.getUserType() == 7){//包保  督导
			//包保责任人数量
			QueryWrapper<UserDO> qw = new QueryWrapper<>();
			qw.select("user_id");
			qw.eq("role_id",8);
			if(user.getUserLevel() == 1){//县

			} else if(user.getUserLevel() == 2){//镇
				qw.eq("town_id",user.getTownId());
			} else if(user.getUserLevel() == 3){//村
				qw.eq("village_id",user.getVillageId());
			}
			long user0Count = userService.count(qw);
			//督导监察人数量

			QueryWrapper<UserDO> qw1 = new QueryWrapper<>();
			qw1.select("user_id");
			if(user.getUserLevel() == 1){//县

			} else if(user.getUserLevel() == 2){//镇
				qw1.eq("town_id",user.getTownId());
			} else if(user.getUserLevel() == 3){//村
				qw1.eq("village_id",user.getVillageId());
			}
			qw1.eq("role_id",9);
			long user1Count = userService.count(qw1);
			//居民数
			QueryWrapper<Citizen> qwCitizen = new QueryWrapper<>();
			qwCitizen.select("id");
			if(user.getUserLevel() == 2){//镇
				qwCitizen.eq("town_id",user.getTownId());
			} else if(user.getUserLevel() == 3){//村
				qwCitizen.eq("village_id",user.getVillageId());
			}
			long citizenCount = citizenService.count(qwCitizen);
			//村庄数

			QueryWrapper<Towns> qwVillage = new QueryWrapper<>();
			qwVillage.select("id");
			qwVillage.eq("grid_level",3);
			if(user.getUserLevel() == 2){//镇
				qwVillage.eq("parent_id",user.getTownId());
			} else if(user.getUserLevel() == 3){//村
				qwVillage.eq("id",user.getVillageId());
			}
			long villageCount = townsService.count(qwVillage);
			model.addAttribute("user0Count",user0Count);
			model.addAttribute("user1Count",user1Count);
			model.addAttribute("citizenCount",citizenCount);
			model.addAttribute("villageCount",villageCount);

			int citizenNoSurveyCount = citizenService.getNoSurveyCountByTownId(user.getTownId());
			model.addAttribute("citizenNoSurveyCount",citizenNoSurveyCount);
		} else if(user.getUserType() == 9){//应急局
			//包保责任人数量
			long user0Count = userService.selectCountByColumn("role_id",8);
			//督导监察人数量
			long user1Count = userService.selectCountByColumn("role_id",9);
			//居民数
			long citizenCount = citizenService.count();
			//居民数
			long villageCount = townsService.selectCountByColumn("grid_level",3);
			model.addAttribute("user0Count",user0Count);
			model.addAttribute("user1Count",user1Count);
			model.addAttribute("citizenCount",citizenCount);
			model.addAttribute("villageCount",villageCount);

			int citizenNoSurveyCount = citizenService.getNoSurveyCountByTownId(null);
			model.addAttribute("citizenNoSurveyCount",citizenNoSurveyCount);
		}

		model.addAttribute("userType", user.getUserType());
		model.addAttribute("shopName", "");
		model.addAttribute("controlText", "");
		if(user.getShopId()!=null && user.getShopId() > 0){
			Shop shop = shopService.findById(user.getShopId());
			if(shop!=null){
				model.addAttribute("shopName", shop.getShopName());
				model.addAttribute("controlText", shop.getControlText());
			}
		}
		if (user.getUserType() == 7 || user.getUserType() == 9) {
			return "main_yingji";
		} else if (user.getUserType() == 10) {
			return "main_danger";
		} else if(user.getUserType() == 2 || user.getUserType() == 3  || user.getUserType() == 4){
			return "main_group";
		}
		return "main";
	}

	@Log("首页的echart 数据")
	@PostMapping("/maindata")
	@ResponseBody
	R maindata() {
		Calendar  calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH) + 1;
		List<String> days = new ArrayList<>();
		List<Long> record_1 = new ArrayList<>();
		List<Long> record_2 = new ArrayList<>();
		for (int i = 0;i < day; i++){
			days.add( month + "-" + (i + 1));
			calendar.set(Calendar.DAY_OF_MONTH,i + 1);
			calendar.set(Calendar.HOUR_OF_DAY,0);
			calendar.set(Calendar.MINUTE,0);
			calendar.set(Calendar.SECOND,0);
			Date begin_time = calendar.getTime();

			calendar.set(Calendar.HOUR_OF_DAY,23);
			calendar.set(Calendar.MINUTE,59);
			calendar.set(Calendar.SECOND,59);
			Date end_time = calendar.getTime();

			QueryWrapper<TerminalCheckRecord> qw = new QueryWrapper<>();
			qw.select("id");
			qw.eq("check_state",1);//正常的
			qw.eq("is_del",0);//正常的
			qw.between("create_time",begin_time,end_time);
			long recordCount1 = terminalCheckRecordService.selectCountByQueryWrapper(qw);
			record_1.add(recordCount1);

			QueryWrapper<TerminalCheckRecord> qw1 = new QueryWrapper<>();
			qw1.select("id");
			qw1.eq("check_state",2);//报修的
			qw1.eq("is_del",0);//正常的
			qw1.between("create_time",begin_time,end_time);
			long recordCount2 = terminalCheckRecordService.selectCountByQueryWrapper(qw1);
			record_2.add(recordCount2);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("days",days);
		jsonObject.put("record_count_1",record_1);
		jsonObject.put("record_count_2",record_2);

		List<String> months = new ArrayList<>();
		List<Long> record_month_1 = new ArrayList<>();
		List<Long> record_month_2 = new ArrayList<>();
		for(int i = 0;i < 12; i++){
			months.add((i + 1) + "月");
			Date begin_time = DateUtils.getDayOfMonth(i + 1,0,0);
			Date end_time = DateUtils.getDayOfMonth(i + 1,1,0);
			QueryWrapper<TerminalCheckRecord> qw = new QueryWrapper<>();
			qw.select("id");
			qw.eq("check_state",1);//正常的
			qw.eq("is_del",0);//正常的
			qw.between("create_time",begin_time,end_time);
			long recordCount1 = terminalCheckRecordService.selectCountByQueryWrapper(qw);
			record_month_1.add(recordCount1);

			QueryWrapper<TerminalCheckRecord> qw1 = new QueryWrapper<>();
			qw1.select("id");
			qw1.eq("check_state",2);//报修的
			qw1.eq("is_del",0);//正常的
			qw1.between("create_time",begin_time,end_time);
			long recordCount2 = terminalCheckRecordService.selectCountByQueryWrapper(qw1);
			record_month_2.add(recordCount2);
		}
		jsonObject.put("months",months);
		jsonObject.put("record_month_1",record_month_1);
		jsonObject.put("record_month_2",record_month_2);
		return R.ok(jsonObject);
	}


	@Log("首页的echart 数据")
	@PostMapping("/mainDataYingji")
	@ResponseBody
	R mainDataYingji(String beginTime,String endTime) {

		UserDO user = getUser();
		List<String> townsName = new ArrayList<>();
		List<Long> recordsCount0 = new ArrayList<>();
		List<Long> recordsCount1 = new ArrayList<>();
		QueryWrapper<Towns> qw = new QueryWrapper<>();
		if (StringUtils.isBlank(beginTime)) {
			beginTime = "2022-11-01 00:00:00";
		} else {
			beginTime = beginTime + " 00:00:00";
		}
		if (StringUtils.isBlank(endTime)) {
			endTime = DateUtils.getTime(new Date());
		} else {
			endTime = endTime + " 23:59:59";
		}
		if (user.getUserType() == 7 || user.getUserType() == 8) {//包保  督导
			qw.eq("parent_id",user.getTownId());
			qw.eq("grid_level",3);
		}else{
			qw.eq("grid_level",2);
		}
		qw.orderByAsc("sort");
		List<Towns> towns = townsService.list(qw);
		if(CollUtil.isNotEmpty(towns)){
			for (Towns town : towns){
				townsName.add(town.getTownName());
				QueryWrapper<SurveyUserRecord> qwRecord = new QueryWrapper<>();
				qwRecord.select("id");
				if(user.getUserType() == 7 || user.getUserType() == 8) {//包保  督导
					qwRecord.eq("village_id", town.getId());
				}else{
					qwRecord.eq("town_id", town.getId());
				}
				qwRecord.eq("check_type",0);
				qwRecord.between("create_time",beginTime, endTime);
				long recordCount0 = surveyUserRecordService.count(qwRecord);
				recordsCount0.add(recordCount0);

				QueryWrapper<SurveyUserRecord> qwRecord1 = new QueryWrapper<>();
				qwRecord1.select("id");
				if(user.getUserType() == 7 || user.getUserType() == 8) {//包保  督导
					qwRecord1.eq("village_id", town.getId());
				}else{
					qwRecord1.eq("town_id", town.getId());
				}
				qwRecord1.eq("check_type",1);

				qwRecord1.between("create_time",beginTime, endTime);

				long recordCount1 = surveyUserRecordService.count(qwRecord1);
				recordsCount1.add(recordCount1);
			}
		}

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("townsName",townsName);
		jsonObject.put("recordsCount0",recordsCount0);
		jsonObject.put("recordsCount1",recordsCount1);
		return R.ok(jsonObject);
	}
}
